package com.lxb.store.dao;

import com.lxb.store.entity.BuyGoods;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.List;

public interface BuyGoodsDao extends JpaRepository<BuyGoods,Integer>,PagingAndSortingRepository<BuyGoods,Integer>,JpaSpecificationExecutor<BuyGoods>{


    @Query(nativeQuery = true,value = "SELECT * FROM  buy_goods WHERE  shop_car_id = ?1 AND name = ?2 AND color = ?3")
    BuyGoods getByShopCarIdAndNameAndColor(Integer shopCarId,String name,String color);

    void deleteById(Integer id);

    @Modifying
    @Transactional
    @Query(nativeQuery = true,value = "UPDATE buy_goods SET num = num +1 WHERE  id = ?1")
    int addBuyGoodsNum(Integer id);

    @Modifying
    @Transactional
    @Query(nativeQuery = true,value = "UPDATE buy_goods SET num = num -1 WHERE  id = ?1")
    int subBuyGoodsNum(Integer id);


    @Modifying
    @Query("delete from BuyGoods b where  b.id in (:ids)")
    @Transactional
    void deleteBuyGoodsById(@Param("ids")List<Integer> ids);

}
